package com.zara.store.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;

public class OfadHelper {

	public OfadVO createOfadFromXml(File file) throws FileNotFoundException, IOException {
		XStream xstream = createXstream();
		FileInputStream is = null;
		try {
			is = new FileInputStream(file);
			OfadVO ofadVO = (OfadVO)xstream.fromXML(is);
			return ofadVO;
		} finally {
			if(is != null) {
				is.close();
			}
		}
	}

	public OfadVO createOfadFromXml(String xml) {
		XStream xstream = createXstream();
		OfadVO ofadVO = (OfadVO)xstream.fromXML(xml);
		return ofadVO;
	}

	private XStream createXstream() {
		XStream xstream = new XStream();
	
		xstream.addDefaultImplementation(ArrayList.class, Collection.class);
		//xstream.addDefaultImplementation(java.sql.Date.class, java.util.Date.class);
		xstream.registerConverter(new DateConverter("yyyy-MM-dd HH:mm:ss.SSS", new String[] { }));
				
		xstream.alias("ofad", OfadVO.class);
		xstream.alias("product", ProductVO.class);
		return xstream;
	}

}
